package gui;

/**
 * @author Ramy Refky 
 * */
public class GrouperComponentGraphics extends ComponentGraphics {

	private static final long serialVersionUID = 1L;
	
	int splitCount;
	int option;
	
	/**
	 * Constructor initializes GROUPER component by initializing ComponentGraphics, and then painting the GrouperComponentLabel.
	 * @param MainGui, XLocation, YLocation, Width, Height, NumberOfOutputs, splitCount, option
	 * */
	public GrouperComponentGraphics(MainGui mg,int originalXLoc,int originalYLoc,
			int originalWidth,int originalHeight,int numOfOutputs,int splitCount,int option){
		
		super(mg,originalXLoc,originalYLoc,originalWidth,originalHeight,numOfOutputs,numOfOutputs);
		
		this.splitCount=splitCount;
		this.option=option;
		
		setSplitInputPins();
		setSplitOutputPins();
		
		this.remove(compLabel);
		compLabel=new GrouperComponentLabel(newXLoc,newYLoc,originalWidth,originalHeight);
		this.add(compLabel);
		setComponentZOrder(compLabel,0);
		repaint();
	
	}
	/**
	 * Gets the total number of grouped wires from the top left group.
	 * @return int
	 * */
	public int getSplitCount(){
		return splitCount;
	}
	/**
	 * Gets the option or type of this Grouper component. If option =1 draw
	 * component with top left group and output group. If option =2 draw
	 * component with bottom left group only. Normally a displayed grouper
	 * component is actually two overlapping components with different options.
	 * @return int
	 * */
	public int getOption(){
		return option;
	}
	@Override
	public void setOutputPins(int num){
	//do nothing
	}
	@Override
	public void setInputPins(int num){
	//do nothing	
	}
	@Override
	public int getNumberOfInputs(){
		return getSplitCount();
	}
	@Override
	public void adjustPins(){
		
		if(option==1){
			int outputCount=getNumberOfOutputs();
			for(int i=1;i<=outputCount;i++){
				int h = newHeight/(2) -1;
				outputPins.get(i-1).zoomAdjust(newWidth*4/5,h,newWidth,h);
			}
			for(int i=0;i<getSplitCount();i++){
				inputPins.get(i).zoomAdjust(0,newHeight/3-2,newWidth/5,newHeight/3-2);
			}
		}		
		if(option==2){
			for(int i=0;i<getSplitCount();i++){
				inputPins.get(i).zoomAdjust(0,newHeight*2/3-1,newWidth/5,newHeight*2/3-1);
			}	
		}
		repaint();
	}
	/**
	 * Initializes the output ComponentPins and positions them accordingly.
	 * */
	public void setSplitOutputPins(){
		
		if(option==1){
			if(numOfOutputs<=1){
				if(numOfOutputs%2==1){
					for(int i=1;i<=numOfOutputs;i++){
						int h = i*originalHeight/(numOfOutputs+1)-1;
						ComponentPin pin = new ComponentPin(mg,this,originalWidth*4/5,h,originalWidth,h);
						outputPins.add(pin);
						pin.setTypeAsOutput();
						this.add(outputPins.get(outputPins.size()-1));
					}
				}
				else{
					for(int i=1;i<=numOfOutputs;i++){
						int h = (2*(i-1)+1)*originalHeight/(numOfOutputs*2) -1;
						ComponentPin pin = new ComponentPin(mg,this,originalWidth*4/5,h,originalWidth,h);
						outputPins.add(pin);
						pin.setTypeAsOutput();
						this.add(outputPins.get(outputPins.size()-1));
					}
				}
			}
			else{
				for(int i=1;i<=numOfOutputs;i++){
					int h = originalHeight/(2) -1;
					ComponentPin pin = new ComponentPin(mg,this,originalWidth*4/5,h,originalWidth,h);
					pin.setAsGrouped();
					pin.setTypeAsOutput();
					outputPins.add(pin);
					this.add(outputPins.get(outputPins.size()-1));
				}
			}
			repaint();
		}
	}
	/**
	 * Initializes the input ComponentPins and positions them accordingly.
	 * */
	public void setSplitInputPins(){
		if(option==1){
			
			for(int i=0;i<getSplitCount();i++){
				ComponentPin pin = new ComponentPin(mg,this,0,originalHeight/3-2,originalWidth/5,originalHeight/3-2);
				if(getSplitCount()>1){
					pin.setAsGrouped();
				}
				inputPins.add(pin);
				this.add(inputPins.get(inputPins.size()-1));
				
			}
		}
		if(option==2){
			
			for(int i=0; i<getSplitCount();i++){
				ComponentPin pin = new ComponentPin(mg,this,0,getHeight()*2/3-1,getWidth()/5,getHeight()*2/3-1);
				if(splitCount>1){
					pin.setAsGrouped();
				}
				inputPins.add(pin);
				this.add(inputPins.get(inputPins.size()-1));	
			}
		}
		repaint();
	}
	
}
